home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / mymenv-notify 2.0 / myAEvents.cc < prev    next >
Text File  |  1996-06-04  |  5KB  |  146 lines

  1. /*
  2.  ************************************************************************
  3.  *         Handling of Mandatory Apple Events
  4.  *        OpenApplication, {Open|Print}Document, Quit
  5.  */
  6.  
  7.  #include "mymenv.h"
  8.  #include "myAEvents.h"
  9.  #include <string.h>
  10.  
  11.             // It is this handler that is called by AEProcessAppleEvent
  12.             // Reply noErr if the event is handled successfully.
  13.             // ot Eventnothandled it's beyond us
  14. pascal OSErr AppleEventGenericHandler::zero_level_handler
  15.     (AppleEvent *_theAppleEvent, AppleEvent *_reply, long handlerRefcon)
  16. {
  17.   assert( handlerRefcon != 0 );
  18.   AppleEventGenericHandler& handler = *(AppleEventGenericHandler *)handlerRefcon;
  19.   assert( handler.signature == Signature_value );
  20.   handler.theAppleEvent = _theAppleEvent;
  21.   handler.reply = _reply;
  22.   return handler();
  23. }
  24.  
  25.             // Install the event handler
  26.             // Should've checked that no other event handler wasn't
  27.             // installed for that event using
  28.             // AEGetEventHandler()
  29.             // We pass the ptr to the object as a refcon
  30. AppleEventGenericHandler::AppleEventGenericHandler(const AEEventID _event_id)
  31.     : signature(Signature_value), event_class(kCoreEventClass), 
  32.       event_id(_event_id)
  33. {
  34.   zero_level_handler_descr = NewAEEventHandlerProc(zero_level_handler);
  35.   do_well( AEInstallEventHandler(event_class,event_id,zero_level_handler_descr,
  36.                    (long)this, FALSE) );
  37. }
  38.  
  39.             // Uninstall the event handler
  40. AppleEventGenericHandler::~AppleEventGenericHandler(void)
  41. {
  42.   do_well( AERemoveEventHandler(event_class,event_id,zero_level_handler_descr,FALSE) );
  43. }
  44.  
  45.             // Check to make sure we got all required params
  46. Boolean AppleEventGenericHandler::got_all_params(void)
  47. {
  48.   DescType    typeCode;        // we don't use them, actually
  49.   Size        actualSize;
  50.  
  51.   OSErr err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  52.               typeWildCard, &typeCode, 0, 0, &actualSize);
  53.   if( err == errAEDescNotFound )    // we got all the required params: all is ok
  54.     return TRUE;
  55.   else if( err == noErr )
  56.     return FALSE;
  57.   else
  58.     do_well( err );
  59.   return FALSE;
  60. }
  61.  
  62.             // Get Descriptor of a list as a direct param
  63. void AppleEventGenericHandler::get_direct_list(AEDescList& list_desc)
  64. {
  65.   do_well( AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &list_desc) );
  66. }
  67.  
  68.             // Get a full path from an alias record
  69.             // Returns a pointer to a static string
  70.             // Note, alias has to be a full alias
  71.             // (minimal alias doesn't have directories record)
  72. const char * get_full_path(AliasHandle alias_handle)
  73. {
  74.   static char path[700];        // Full path to return
  75.   Str63 buffer;
  76.                   // Write a volume name at the beginning of
  77.                   // the path
  78.   do_well( GetAliasInfo(alias_handle,asiVolumeName,buffer) );
  79.   memcpy(path,(char *)buffer+1,buffer[0]);
  80.   char * first_col_pos = path + buffer[0];
  81.   *first_col_pos = ':';        // colon after the volume name
  82.   char * after_ldir_pos = first_col_pos+1;
  83.  
  84.                 // Put directory names from the parent upwards
  85.                 // In the string
  86.                 //    Volume_name:Dir3:Dir2:Dir1:
  87.                 // first_col_pos points to the first : after
  88.                 // the volume name, after_ldir_pos points
  89.                 // after the last column. The new directory
  90.                 // retrieved from the alias record is wedged
  91.                 // between the volume name and after_vname_pos
  92.   for(register int dir_level=1;;dir_level++)
  93.   {
  94.     do_well( GetAliasInfo(alias_handle,dir_level,buffer) );
  95.     if( buffer[0] == 0 )
  96.       break;                // no more (grand...dad) directories
  97.                     // Move [after_vname_pos,after_ldir_pos)
  98.                     // chunk forward to make room for this
  99.                     // (grand)parent directory
  100.     char * p = after_ldir_pos;
  101.     char * q = after_ldir_pos += buffer[0]+1;    // reserve space for :
  102.     assert( after_ldir_pos < path + sizeof(path) );
  103.     while( p > first_col_pos )
  104.       *--q = *--p;
  105.     p = (char *)buffer+1+buffer[0];    // End of the (grand)parent dir name
  106.     while( q > first_col_pos+1 )
  107.       *--q = *--p;
  108.   }
  109.                 // Get the file name itself and append it to the
  110.                 // end
  111.   do_well( GetAliasInfo(alias_handle,asiAliasName,buffer) );
  112.   assert( after_ldir_pos+buffer[0]+1 < path + sizeof(path) );
  113.   memcpy(after_ldir_pos,(char *)buffer+1,buffer[0]);
  114.   after_ldir_pos[buffer[0]] = '\0';    // Properly terminate the string
  115.  
  116.   return path;
  117. }
  118.  
  119. #if 0
  120.             // This is the real handler
  121. template <class DocHandler>
  122. virtual OSErr AppleEventOpenDoc<DocHandler>::operator() (void)
  123. {
  124.   get_direct_list(desc_opened_doc);
  125.   assert( got_all_params() );
  126.   do_well( AECountItems(&desc_opened_doc, &no_opened_doc) );
  127.   OSErr err_sofar = noErr;
  128.   DocHandler doc_handler(no_opened_doc);
  129.   int want_handle = 1;
  130.   assert( want_handle <= no_opened_doc );
  131.   for(register int index=1; err_sofar == noErr && index <= want_handle; index++)
  132.   {
  133.     Size actual_size;
  134.     AEKeyword keyword;
  135.     DescType type_code;
  136.     do_well( AEGetNthPtr(&desc_opened_doc, index, typeFSS, &keyword, &type_code,
  137.                     (Ptr)&file_spec, sizeof(file_spec), &actual_size) );
  138.     if( !doc_handler(file_spec) )
  139.       err_sofar = errAEEventNotHandled;
  140.    }    
  141.   do_well( AEDisposeDesc(&desc_opened_doc) );
  142.  
  143.   return err_sofar;
  144. }
  145. #endif
  146.